<?php

namespace IDock\Insurance\JTIC\Request;

use IDock\Insurance\JTIC\JTICEnums;
use IDock\Insurance\JTIC\Request\AllJTICRequest;
use IDock\Insurance\JTIC\Response\NonCarProductListResponse;
use IDock\Visitor\NonCar\NonCarProductListVisitor;

class NonCarProductListRequest extends AllJTICRequest
{
    public $code = 'V206';

    protected $urlTip = '非车产品列表';

    protected $response = NonCarProductListResponse::class;

    /*** @var NonCarProductListVisitor $visitor */
    protected $visitor;

    public function generate(): array
    {
        $motorUsageTypeCode = '';
        JTICEnums::getUsageTypeCode(
            $this->visitor->useNatureCode,
            $this->visitor->causeType,
            $this->visitor->carKindCode,
            $this->visitor->seats,
            $this->visitor->vehicleTonnage,
            $motorUsageTypeCode,
            $motorTypeCode
        );
        //var_dump($this->visitor->insuredVisitor);
        return [
            'BasePart' => [
                "NonvhlProdType" => 'ACC',
                'ProjectCode' => '',
                'ProdGroup' => 'A335',//
                'DeptTeamCode' => $this->visitor->argc['DeptTeamCode'] ?? '',
                'PeriodUnit' => 'IU01',//单位 年
                'PeriodInsur' => '1',//期限
                'InsuredAge' => $this->getAgeFromIdCard($this->visitor->insuredVisitor->certificateNo),//被保人年龄
                'VehicleOwnerType' => JTICEnums::$relationTypeCode[$this->visitor->ownerVisitor->typeCode] ?? '',//车主性质代码
                'MotorUsageTypeCode' => $motorUsageTypeCode,// 非营业个人
                'MotorTypeCode' => $motorTypeCode,//6-10座
                'VehicleSeat' => $this->visitor->seats,
            ],
        ];
    }

    public function getAgeFromIdCard($idCard)
    {
        if (strlen($idCard) != 18) {
            return 0; // 身份证号码必须是18位
        }

        $year = (int)substr($idCard, 6, 4); // 获取出生年份
        return date('Y') - $year + (date('md') < substr($idCard, 10, 4) ? 0 : 1);
    }
}
